<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-19 16:47
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class getServiceListCriteria extends BaseCriterias
{
	private $params;

	public function __construct($userId, $storeId, $status = 1)
	{
		$this->params = [
			'store_id' => $storeId,
			'user_id'  => $userId,
			'status'   => $status ? true : false
		];
	}

	public function apply($model, RepositoryInterface $repository)
	{
		$storeId = $this->params['store_id'];
		$where['store_id'] = $this->params['store_id'];
		if(!empty($this->params['user_id'])){
			$where['user_id'] = $this->params['user_id'];
		}

		$list = $model->with([
			'storeService' => function ($v) use ($storeId) {
				$v->select(['id', 'name', 'price','type'])->where('store_id', '=', $storeId)->withCount(['appointment'=>function($v){
					$v->whereDate('created_at','=',date('Y-m-d'));
				}])->with(['category'=>function($v){
					$v->select(['dictionary_item.id', 'dictionary_item.fullname', 'picture.image as portrait'])
                    ->leftJoin('picture', 'picture.id', 'resource');
				}]);
			}
		])
			->where($where);
		return $this->params['status'] ? $list->where('status', '=', 1) : $list;
	}
}
